<?php


namespace app\api\controller;


use app\api\logic\OrderLogic;
use app\api\validate\OrderValidate;
use app\common\controller\BaseApiController;
use support\constants\QueueKey;
use support\Container;
use support\service\MqClient;
use support\utils\LockUtil;

/**
 * 订单管理
 * Class OrderController
 * @package app\api\controller
 */
class OrderController extends BaseApiController
{
    protected function init()
    {
        parent::init();
        $this->logic = new OrderLogic();
        $this->validate = new OrderValidate();
    }

    /**
     * 结算页面 计算价格等信息
     */
    public function preCreateOrder()
    {
        $data = $this->logic->preCreateOrder(input());
        return $this->success($data);
    }

    public function createOrder()
    {
        $order_no = "";
        LockUtil::tryLock("create_order:" . request()->uid, function () use (&$order_no) {
            $input = input();
            $this->logic->transaction(function () use ($input, &$order_no) {
                $order_no = $this->logic->createOrder($input);
            });
        });

        //30分钟自动关闭
        Container::get(MqClient::class)->send(QueueKey::CLOSE_ORDER, [
            'order_no' => $order_no
        ], 30 * 60);
        return $this->success([
            'order_no' => $order_no
        ]);
    }

    public function orderSimpleDetail()
    {
        $this->validate->checkData(input(), 'order_no');
        $data = $this->logic->orderSimpleDetail(input('order_no'));
        return $this->success($data);
    }

    /**
     * 确认支付
     */
    public function confirmPayment()
    {
        $this->validate->checkData(input(), 'confirmPayment');
        $data = [];
        LockUtil::tryLock("confirm_payment:" . input('order_no'), function () use (&$data) {
            $this->logic->transaction(function () use (&$data) {
                $data = $this->logic->confirmPayment(input('order_no'), input('pay_way'));
            });
        });
        return $this->success($data);
    }

    /**
     * 订单列表
     */
    public function orderList()
    {
        $data = $this->logic->orderList($this->form);
        return $this->success($data);
    }

    /**
     * 订单详情
     */
    public function orderDetail()
    {
        $data = $this->logic->orderDetail(input('order_no'));
        return $this->success($data);
    }

    /**
     * 关闭订单
     */
    public function cancelOrder()
    {
        $this->validate->checkData(input(), 'cancelOrder');
        $this->logic->transaction(function () {
            $this->logic->cancelOrder(input('order_id'));
        });
        return $this->success();
    }

    /**
     * 立即收货
     */
    public function takeDelivery()
    {
        $this->validate->checkData(input(), 'takeDelivery');
        $data = $this->logic->takeDelivery(input('order_id'));
        return $this->success($data);
    }

    /**
     * 删除订单
     */
    public function deleteOrder()
    {
        $this->validate->checkData(input(), 'deleteOrder');
        $data = $this->logic->deleteOrder(input('order_id'));
        return $this->success($data);
    }
}